/*
 * Copyright (c) 2024, Qi Yaolong.
 * dragon@hbu.edu.cn
 * HeBei University, China.
 * 
 * 简体中文
 * 保留所有权利。除非符合（遵守）以下条款，否则您不得使用此文件：
 * 
 * 禁止任何未经授权的个人或组织以任何形式将本程序及其附属资料、创新、创意、架构设计、算法、衍生作品
 * 等用于任何商业目的、盈利活动、各类竞赛(比赛)、直播教学、录播教学、线下课程、书籍编写、教材编写、
 * 会议、培训、公益活动、项目课题、毕业设计、毕业论文、学术论文等。
 * 
 * 本程序由版权持有人和贡献人“按原样”提供，不提供任何明示或暗示担保，包括但不局限于对适销性和特定
 * 用途适合性的暗示担保。在任何情况下，版权持有人或贡献人对因使用本程序而导致的任何直接、间接、附带、
 * 特殊、典型或后果性损害（包括但不限于购买替代商品或服务；使用损失、数据丢失或利润损失；业务中断）
 * 不承担任何责任，无论其如何产生，也不论其责任理论为何，无论是合同、严格责任还是侵权（包括疏忽或
 * 其他），即使已告知此类损害的可能性。
 * 
 * English
 * All rights reserved. You may NOT use this file except in compliance with the
 * following terms:
 * 
 * IT IS PROHIBITED FOR ANY UNAUTHORIZED PERSON OR ENTITY TO USE THIS PROGRAM AND 
 * ITS ANCILLARY DATA, INNOVATIONS, CONCEPTS, ARCHITECTURE DESIGN, ALGORITHMS, 
 * DERIVED WORKS, ETC., IN ANY FORM FOR COMMERCIAL PURPOSES, PROFIT-MAKING 
 * ACTIVITIES, VARIOUS COMPETITIONS (CONTESTS), LIVE TEACHING, PRE-RECORDED 
 * LESSONS, OFFLINE COURSES, BOOK WRITING, CONFERENCES, TRAININGS, PUBLIC WELFARE 
 * ACTIVITIES, PROJECTS, TOPICS, GRADUATION DESIGNS, DISSERTATIONS, THESES, AND 
 * OTHER APPLICATIONS.
 * 
 * THIS PROGRAM IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS PROGRAM, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>          // Standard input/output functions
#include <unistd.h>         // POSIX standard interface

#include "ohos_init.h"      // Initialization for services and features
#include "cmsis_os2.h"      // CMSIS-RTOS API V2

#include "iot_gpio.h"       // OpenHarmony HAL: GPIO operations
#include "iot_gpio_ex.h"

#include "oled_ssd1306.h"   // Simplified OLED display driver interface

// Define a macro for the ADC2 channel
// #define ANALOG_KEY_CHAN_NAME HI_ADC_CHANNEL_2

// Font data for "万 物 互 联" created using PCtoLCD
// Each character is 16x16 pixels, stored in a 32-byte array
uint8_t fonts1[][32] = {
    {0x04, 0x04, 0x04, 0x04, 0x04, 0xFC, 0x44, 0x44, 0x44, 0x44, 0x44, 0xC4, 0x04, 0x04, 0x04, 0x00, 0x80, 0x40, 0x20, 0x18, 0x06, 0x01, 0x00, 0x00, 0x40, 0x80, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00}, /*"万",0*/
    {0x40, 0x3C, 0x10, 0xFF, 0x10, 0x10, 0x20, 0x10, 0x8F, 0x78, 0x08, 0xF8, 0x08, 0xF8, 0x00, 0x00, 0x02, 0x06, 0x02, 0xFF, 0x01, 0x01, 0x04, 0x42, 0x21, 0x18, 0x46, 0x81, 0x40, 0x3F, 0x00, 0x00}, /*"物",1*/
    {0x00, 0x02, 0x02, 0x02, 0xE2, 0x3E, 0x22, 0x22, 0x22, 0x22, 0x22, 0xE2, 0x02, 0x02, 0x00, 0x00, 0x40, 0x40, 0x40, 0x47, 0x44, 0x44, 0x44, 0x44, 0x44, 0x74, 0x4E, 0x41, 0x40, 0x40, 0x40, 0x00}, /*"互",2*/
    {0x02, 0xFE, 0x92, 0x92, 0xFE, 0x02, 0x00, 0x10, 0x11, 0x16, 0xF0, 0x14, 0x13, 0x10, 0x00, 0x00, 0x10, 0x1F, 0x08, 0x08, 0xFF, 0x04, 0x81, 0x41, 0x31, 0x0D, 0x03, 0x0D, 0x31, 0x41, 0x81, 0x00}, /*"联",3*/
};

// Font data for "万 物 互 联 电 压" created using an online tool (https://www.23bei.com/tool-965.html)
// Each character is 16x16 pixels, stored in a 32-byte array
uint8_t fonts2[] = {
    /*-- ID:0,字符:"我",ASCII编码:CED2,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x20,0x24,0x24,0x24,0xfe,0x23,0x22,0x20,0xff,0x20,0x22,0xac,0x20,0x30,0x20,0x00,
    0x00,0x08,0x48,0x84,0x7f,0x02,0x21,0x10,0x09,0x06,0x1a,0x61,0x80,0xe0,0x00,0x00,

    /*-- ID:1,字符:"走",ASCII编码:D7DF,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x40,0x40,0x48,0x48,0x48,0x48,0x48,0xff,0x48,0x48,0x48,0x4c,0x48,0x60,0x40,0x00,
    0x00,0x80,0x40,0x20,0x1f,0x20,0x40,0x7f,0x44,0x44,0x44,0x46,0x44,0x60,0x20,0x00,

    /*-- ID:2,字符:"丢",ASCII编码:B6AA,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x00,0x22,0x22,0x22,0x22,0x22,0xfe,0x21,0x21,0x21,0x31,0x20,0x00,0x00,0x00,
    0x02,0x02,0x42,0x62,0x52,0x4a,0x46,0x43,0x42,0x42,0x52,0x62,0xc2,0x03,0x02,0x00,

    /*-- ID:3,字符:"失",ASCII编码:CAA7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x40,0x20,0x1e,0x10,0x10,0x10,0xff,0x10,0x10,0x10,0x18,0x10,0x80,0x00,0x00,
    0x01,0x81,0x81,0x41,0x21,0x11,0x0d,0x03,0x0d,0x11,0x21,0x21,0x41,0xc1,0x41,0x00,

    /*-- ID:4,字符:"了",ASCII编码:C1CB,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x02,0x02,0x02,0x02,0x02,0x02,0xe2,0x22,0x12,0x0a,0x06,0x02,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    /*-- ID:5,字符:"星",ASCII编码:D0C7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x00,0xbe,0x2a,0x2a,0x2a,0x2a,0xea,0x2a,0x2a,0x2a,0x2a,0xbf,0x02,0x00,0x00,
    0x48,0x44,0x4b,0x49,0x49,0x49,0x49,0x7f,0x49,0x49,0x49,0x4d,0x49,0x61,0x40,0x00,

    /*-- ID:6,字符:"闪",ASCII编码:C9C1,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0xf8,0x01,0x02,0x06,0x00,0x02,0xf2,0x02,0x02,0x02,0x02,0x02,0xff,0x02,0x00,
    0x00,0xff,0x00,0x00,0x08,0x04,0x03,0x00,0x01,0x02,0x0c,0x40,0x80,0x7f,0x00,0x00,
    
    /*-- ID:7,字符:"连",ASCII编码:C1AC,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x40,0x42,0xcc,0x00,0x04,0x44,0x64,0x5c,0x47,0xf4,0x44,0x64,0x46,0x04,0x00,0x00,
    0x40,0x20,0x1f,0x20,0x44,0x44,0x44,0x44,0x44,0x7f,0x44,0x44,0x46,0x64,0x20,0x00,

    /*-- ID:8,字符:"接",ASCII编码:BDD3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x10,0x10,0x10,0xff,0x90,0x54,0x44,0x54,0xe5,0x46,0x64,0x54,0x46,0x44,0x00,0x00,
    0x02,0x42,0x81,0x7f,0x02,0x02,0x82,0x8a,0x57,0x22,0x32,0x4e,0xc2,0x03,0x02,0x00,
    
    /*-- ID:9,字符:"成",ASCII编码:B3C9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x00,0xf8,0x88,0x88,0x88,0x88,0x08,0x7f,0x88,0x0a,0x0c,0x08,0xc8,0x00,0x00,
    0x40,0x20,0x1f,0x00,0x08,0x10,0x0f,0x40,0x20,0x13,0x1c,0x24,0x43,0x80,0xf0,0x00,
    
    /*-- ID:10,字符:"功",ASCII编码:B9A6,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x08,0x08,0x08,0xf8,0x0c,0x28,0x20,0x20,0xff,0x20,0x20,0x20,0x20,0xf0,0x20,0x00,
    0x08,0x18,0x08,0x0f,0x84,0x44,0x20,0x1c,0x03,0x20,0x40,0x80,0x40,0x3f,0x00,0x00
};

 
void OledShowChinese1(uint8_t x, uint8_t y, uint8_t idx)
{
    uint8_t t;

    OledSetPosition(x, y);
    for (t = 0; t < 16; t++)
    {
        WriteData(fonts1[idx][t]);
    }

    OledSetPosition(x, y + 1);
    for (t = 16; t < 32; t++)
    {
        WriteData(fonts1[idx][t]);
    }
}

 
void OledShowChinese2(uint8_t x, uint8_t y, uint8_t idx)
{
    uint8_t t;

    OledSetPosition(x, y);
    for (t = 0; t < 16; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }

    OledSetPosition(x, y + 1);
    for (t = 16; t < 32; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }
}

 
void OledTask(void)
{
    OledInit();
    OledFillScreen(0x00);

    OledShowChinese2(11 +  0, 3, 5); // 星
    OledShowChinese2(11 + 18, 3, 6); // 闪
    OledShowChinese2(11 + 36, 3, 7); // 连
    OledShowChinese2(11 + 54, 3, 8); // 接
    OledShowChinese2(11 + 72, 3, 9); // 成
    OledShowChinese2(11 + 90, 3, 10); // 功
}